package jok.io;

import java.io.File;
import java.io.FileWriter;
import java.io.IOException;

public class FileWriterDemo {

	public static void main(String[] args) {

		// writeFile();
		// writeFile2();
		writeFile3();

		FileDemo.deleteFile("D:\\a.txt");
	}

	public static void show() {
		FileWriter fw = null;
		try {
			fw = new FileWriter(new File("D:/a.txt"));
			fw = new FileWriter("D:/a.txt");
			fw = new FileWriter(new File("D:/a.txt"), true);
			fw = new FileWriter("D:/a.txt", true);
		} catch (IOException e) {
			e.printStackTrace();
		} finally {
			try {
				if (fw != null)
					fw.close();
			} catch (IOException e) {
				e.printStackTrace();
			}
		}
	}

	public static void writeFile() {
		FileWriter fw = null;
		try {
			fw = new FileWriter(new File("D:/a.txt"));
			fw.write("HelloWorld");
			fw.flush();
		} catch (IOException e) {
			e.printStackTrace();
		} finally {
			try {
				if (fw != null)
					fw.close();
			} catch (IOException e) {
				e.printStackTrace();
			}
		}
	}

	public static void writeFile2() {
		FileWriter fw = null;
		try {
			fw = new FileWriter(new File("D:/a.txt"));
			for (int i = 0; i < 100; i++) {
				fw.write("HelloWorld" + "\r\n");
				if (i % 10 == 0) {
					fw.flush(); // 每10条flush一次
				}
			}
			fw.flush();// 最后剩余的再flush一次
		} catch (IOException e) {
			e.printStackTrace();
		} finally {
			try {
				if (fw != null)
					fw.close();
			} catch (IOException e) {
				e.printStackTrace();
			}
		}
	}

	public static void writeFile3() {
		FileWriter fw = null;
		try {
			fw = new FileWriter(new File("D:/a.txt"));
			char[] cs = { 'a', 'b', 'c', 'd' };
			fw.write(cs);
			fw.write(cs, 1, 2);
			fw.write(105);// int 对应ASCII码 字母 i
			fw.write("105");
			fw.write("HelloWorld", 5, 2);
			fw.flush();// 写入内容：abcdbci105Wo
		} catch (IOException e) {
			e.printStackTrace();
		} finally {
			try {
				if (fw != null)
					fw.close();
			} catch (IOException e) {
				e.printStackTrace();
			}
		}
	}
}
